package com.google.code.jholidays.io;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

import com.google.code.jholidays.core.DescriptorCollection;
import com.google.code.jholidays.io.xml.XmlReader;


public class XmlReaderTest {

    @Test
    public void testReadNonExistingFile() throws IOException {
	File f = File.createTempFile("tmp", "000");

	String fileName = f.getAbsolutePath();

	f.delete();

	XmlReader reader = new XmlReader(fileName);

	assertNull(reader.read());
    }

    @Test
    public void testReadEmptyFile() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	XmlReader reader = new XmlReader(f.getAbsolutePath());

	assertNull(reader.read());
    }

    @Test
    public void testReadWellFormedFile() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	FileWriter writer = new FileWriter(f);
	writer.write(XmlReaderHelper.getWellFormedXml());
	writer.close();

	XmlReader reader = new XmlReader(f.getAbsolutePath());
	DescriptorCollection collection = reader.read();

	assertNotNull(collection);
	assertEquals(3, collection.size());

    }

    @Test
    public void testReadMissingNameFile() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	FileWriter writer = new FileWriter(f);
	writer.write(XmlReaderHelper.getMissingNameElementXml());
	writer.close();

	XmlReader reader = new XmlReader(f.getAbsolutePath());
	DescriptorCollection collection = reader.read();

	assertNull(collection);
    }

    @Test
    public void testReadMissingDecriptionFile() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	FileWriter writer = new FileWriter(f);
	writer.write(XmlReaderHelper.getMissingDescriptionElementXml());
	writer.close();

	XmlReader reader = new XmlReader(f.getAbsolutePath());
	DescriptorCollection collection = reader.read();

	assertNull(collection);
    }

    @Test
    public void testReadMissingParentIDFile() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	FileWriter writer = new FileWriter(f);
	writer.write(XmlReaderHelper.getMissingParentIDElementXml());
	writer.close();

	XmlReader reader = new XmlReader(f.getAbsolutePath());
	DescriptorCollection collection = reader.read();

	assertNull(collection);
    }

    @Test
    public void testReadMissingExpressionElement() throws IOException {
	File f = File.createTempFile("tmp", "000");
	f.deleteOnExit();

	FileWriter writer = new FileWriter(f);
	writer.write(XmlReaderHelper.getMissingExpressionElementXml());
	writer.close();

	XmlReader reader = new XmlReader(f.getAbsolutePath());
	DescriptorCollection collection = reader.read();

	assertNull(collection);
    }
}
